import re

f = open("../config.txt",encoding='utf-8')
configContent = f.read()
f.close()

first = re.findall("FieldNames=''", configContent)
if len(first) == 1:
    configContent = re.compile(first[0]).sub("FieldNames=' '", configContent)
f = open("../config.txt",'w',encoding='utf-8')
f.write(configContent)
f.close()    

# import sys
# sys.exit(0)


# 读取配置文件------------------------------------------------------------------------------------------------

f = open("../config.txt",encoding='utf-8')
configContent = f.read()
f.close()


findArr = [
    "dbUser='[\w]+'","dbPasswd='[\w]+'","tableName='[\w]+'","keyName='[\w]+'","keyNextName='[\w]+'",
    "dJudgeNum='[\w]+'","oneColRows='[\w]+'","FieldNames='[\w\s,]+'","outputName='[\w]+'",
]


configArr = [0]*9  
for index, item in enumerate(findArr):
    first = re.findall(item, configContent)[0]
    second = re.findall("'[\w\s,]+'",first)[0]
    configArr[index] = re.findall("[\w\s,]+",second)[0]

# for i in configArr:
#     print(i)
# import sys
# sys.exit(0)

dbUser = configArr[0]
dbPasswd=configArr[1]
tableName = configArr[2]
keyName = configArr[3]
keyNextName = configArr[4]
dJudgeNum=configArr[5]
oneColRows=configArr[6]
FieldNames=configArr[7]
outputName=configArr[8]






#修改java配置文件------------------------------------------------------------------------------------------
findArr = [
    "db.username=[\w]+","db.password=[\w]+","tablename=[\w]+","filename=[\w]+"
]

replaceArr = [
"db.username="+dbUser,"db.password="+dbPasswd,"tablename="+tableName,"filename=d_"+outputName
]

# for i in replaceArr:
#     print(i)
# import sys
# sys.exit(0)

f = open("../java/application-db.properties",encoding='utf-8')
javaConfig = f.read()
f.close()


for index, item in enumerate(findArr):
    javaConfig = re.compile(item).sub(replaceArr[index], javaConfig)
f = open("../java/application-db.properties",'w',encoding='utf-8')
f.write(javaConfig)
f.close()    




#修改java2配置文件------------------------------------------------------------------------------------------
findArr = [
    "db.username=[\w]+","db.password=[\w]+","tableName=[\w]+","fileName=[\w]+",
    "manyRowsInColumn=[\w]+","FieldNames=[\w\s,]+"
]

replaceArr = [
"db.username="+dbUser,"db.password="+dbPasswd,"tableName="+tableName,"fileName=d_"+outputName+"_query",
"manyRowsInColumn="+oneColRows,"FieldNames="+FieldNames+'\n'
]

# for i in replaceArr:
#     print(i)
# import sys
# sys.exit(0)

f = open("../java2/application-db.properties",encoding='utf-8')
javaConfig = f.read()
f.close()


for index, item in enumerate(findArr):
    javaConfig = re.compile(item).sub(replaceArr[index], javaConfig)

# print(javaConfig)
# import sys
# sys.exit(0)

f = open("../java2/application-db.properties",'w',encoding='utf-8')
f.write(javaConfig)
f.close()    

